package kr.ac.kongju.gedl.index.web;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import kr.ac.kongju.gedl.cmmn.web.CU;
import kr.ac.kongju.gedl.code.vo.CodeInfoVO;
import kr.ac.kongju.gedl.common.util.ListResultVO;
import kr.ac.kongju.gedl.item.service.ItemService;
import kr.ac.kongju.gedl.item.vo.ItemVO;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.support.SessionStatus;

import egovframework.rte.fdl.property.EgovPropertyService;
import egovframework.rte.ptl.mvc.tags.ui.pagination.PaginationInfo;

@Controller
public class IndexController {
	
	/** EgovPropertyService */
    @Resource(name = "propertiesService")
    protected EgovPropertyService propertiesService;

    @Resource(name = "itemService")
    protected ItemService itemService;
    
    
    private String DEFAULT_AREA_CD = "R015";
    private String DEFAULT_IDX = "IDX001";
    private String DEFAULT_IDX_TYPE = "01";
    private final int PAGE_SIZE = 5;
    private final int ROW_PER_PAGE = 17;
    
    
    @RequestMapping(value="/item/areaService.do")
	public String selectItemListByArea(@ModelAttribute("searchVO") ItemVO searchVO, 
			ModelMap model, SessionStatus status, HttpSession session) throws Exception {
    	
    	searchVO.setPageSize(PAGE_SIZE);
    	searchVO.setRowPerPage(ROW_PER_PAGE);
    	
    	ListResultVO<ItemVO> resultList = itemService.retrieveItemList(searchVO);
		
    	String areaCd = searchVO.getAreaCd();
    	
    	if(StringUtils.isEmpty(areaCd)) {
    		areaCd = DEFAULT_AREA_CD;
    		searchVO.setAreaCd(areaCd);
    	}
    	
    	List<CodeInfoVO> regionList1 = CU.getInstance().getCodeList("REGION");
    	
    	
    	if(areaCd.length() >= 4) {
    		CodeInfoVO region = CU.getInstance().getCodeInfo(areaCd.substring(0, 4));
    		model.addAttribute("region", region);
    		
	    	List<CodeInfoVO> regionList = CU.getInstance().getCodeList(areaCd.substring(0, 4));
			model.addAttribute("regionList", regionList);
			
			CodeInfoVO subRegion = CU.getInstance().getCodeInfo(areaCd);
			model.addAttribute("subRegion", subRegion);
    	}
		
    	//페이징을 구성한다
    	PaginationInfo paginationInfo = new PaginationInfo();

		paginationInfo.setCurrentPageNo(searchVO.getPage());
		paginationInfo.setRecordCountPerPage(searchVO.getRowPerPage());
		paginationInfo.setPageSize(searchVO.getPageSize());

		paginationInfo.setTotalRecordCount(resultList.getListCount());
    	
		
    	
		model.addAttribute("searchVO", searchVO);
		model.addAttribute("regionList1", regionList1);
		model.addAttribute("resultList", resultList);
		model.addAttribute("paginationInfo", paginationInfo);
		
    	return "/index/itemListByArea";
	}
    
    @RequestMapping(value="/item/indexService.do")
	public String selectItemListByIndex(@ModelAttribute("searchVO") ItemVO searchVO, 
			ModelMap model, SessionStatus status, HttpSession session) throws Exception {
    	
    	if(StringUtils.isEmpty(searchVO.getSearchIdxType())) {
    		searchVO.setSearchIdxType(DEFAULT_IDX_TYPE);
    	}
    	
    	if(StringUtils.isEmpty(searchVO.getSearchIdx())) {
    		searchVO.setSearchIdx(DEFAULT_IDX);
    	}
    	
    	List<CodeInfoVO>idxList = CU.getInstance().getCodeList("IDX");
    	
    	searchVO.setPageSize(PAGE_SIZE);
    	searchVO.setRowPerPage(ROW_PER_PAGE);
    	
    	ListResultVO<ItemVO> resultList = itemService.retrieveItemList(searchVO);
		
    	
    	//페이징을 구성한다
    	PaginationInfo paginationInfo = new PaginationInfo();

		paginationInfo.setCurrentPageNo(searchVO.getPage());
		paginationInfo.setRecordCountPerPage(searchVO.getRowPerPage());
		paginationInfo.setPageSize(searchVO.getPageSize());

		paginationInfo.setTotalRecordCount(resultList.getListCount());
    	
		model.addAttribute("searchVO", searchVO);
		model.addAttribute("resultList", resultList);
		model.addAttribute("idxList", idxList);
		model.addAttribute("paginationInfo", paginationInfo);
		
    	return "/index/itemList";
	}
    
}
